﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Faml
{
    public class Operation
    {
        private string name;
        private MethodInfo method;
        private object instance;

        public Operation(string name, MethodInfo method)
        {
            this.name = name;
            this.method = method;
        }

        public Operation(string name, MethodInfo method, object instance)
        {
            this.name = name;
            this.method = method;
            this.instance = instance;
        }

        public string Name
        {
            get { return name; }
        }

        public MethodInfo Method
        {
            get { return method; }
        }

        public object InstanceObject
        {
            get { return instance; }
            internal set { instance = value; }
        }

        public bool IsStatic
        {
            get { return method.IsStatic; }
        }

        public override int GetHashCode()
        {
            return name.GetHashCode() ^ method.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (!(obj is Operation))
                return false;

            return Equals(this, (Operation)obj);
        }

        private static bool Equals(Operation op1, Operation op2)
        {
            if ((object)op1 == null)
                return (object)op2 == null;

            if ((object)op2 == null)
                return false;

            return op1.Name == op2.Name && op1.method == op2.method;
        }

        public static bool operator ==(Operation op1, Operation op2)
        {
            return Equals(op1, op2);
        }

        public static bool operator !=(Operation op1, Operation op2)
        {
            return !Equals(op1, op2);
        }
    }
}
